package com.yloveg.controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.websocket.Session;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.yloveg.base.BaseController;
import com.yloveg.common.PageUtil;
import com.yloveg.entity.SendMsg;
import com.yloveg.entity.UserInfo;
import com.yloveg.service.SendMsgService;
import com.yloveg.service.UserInfoService;
import com.yloveg.service.UserService;
import com.yloveg.service.WSMessageService;
 
@Controller
@RequestMapping("/message")
public class MessageController extends BaseController{
	private static final Logger logger = LoggerFactory.getLogger(MessageController.class);
    //websocket服务层调用类
    @Autowired
    private WSMessageService wsMessageService;
    
    @Autowired
    private UserInfoService userInfoService; 
    
    @Autowired
    private SendMsgService sendMsgService; 
 
  //请求入口
    @RequestMapping(value="/sengToUser")
    @ResponseBody
    public Map<String, Object> sengToUser(@RequestParam(value="fuserId",required=true) Long fuserId,@RequestParam(value="tuserId",required=true) Long tuserId,
        @RequestParam(value="message",required=true) String message){
    	Map<String, Object> resultMap = new HashMap<String, Object>();
    	SendMsg msg = new SendMsg();
    	msg.setSendId(fuserId);
    	msg.setToUserid(tuserId);
    	msg.setMsgContent(message);
    	UserInfo info = new UserInfo();
    	info.setUserId(fuserId);
    	info = userInfoService.queryOneUserInfo(info);
    	msg.setSendName(info.getName());
        logger.debug("收到{}发送请求，向用户{}的消息：{}",fuserId,tuserId,message);
        if(wsMessageService.sengToUser(fuserId,tuserId, message)){
        	resultMap.put("code", 1);
        	msg.setSendState(1);
        }else{
        	resultMap.put("code", 0);
        	msg.setSendState(0);
        } 
        sendMsgService.insert(msg);
        return resultMap;
    }
    
    @RequestMapping(value="/getOnlineUserInfo")
    @ResponseBody
    public Map<String, Object> getOnlineUserInfo(@RequestParam(value="userId",required=true) Long userId){
    	Map<String, Object> resultMap = new HashMap<String, Object>();
        ArrayList<Long> ids = wsMessageService.getOnlineUser(userId);
        if(ids.size()==0){
        	resultMap.put("code", 0);
        }else{
            resultMap.put("userIds", ids);
            List<UserInfo> resultList = userInfoService.queryUserInfo(resultMap);
            resultMap.put("code", 1);
            resultMap.put("resultList", resultList);
        }

        return resultMap;
    }
    
    @RequestMapping({"/getMsgList"})
    @ResponseBody
    public Map<String, Object> getMsgList(SendMsg msg){
      Map<String, Object> map = PageUtil.dateToLayTable(this.sendMsgService.queryUserMsgPage(1, 10, msg));
      return map;
    }
}
